var uploadToPicasaWebStep=
{
	title:'Upload album to Picasa web',
	createUI:function()
	{
		var ret=$('<div>'+
			'<table id="tblPicasaWebAccount"><thead><tr><td>Account</td><td>Free Space</td></tr></thead><tbody></tbody></table>'+
			'Check space<button id="btnCheckPicasaWebSpace">Check</button><br>'+
			'</div>');
		return ret;
	},
	postCreateUI:function()
	{
		$('#btnCheckPicasaWebSpace').click(function(){uploadToPicasaWebStep.accountInfoFetcher.startFetch()});
	},
	accountInfoFetcher:{
		startFetch:function(){
			$('#tblPicasaWebAccount tbody').empty();
			this.fetchingFromIndex=2;
			this.continueFetch();
		},
		continueFetch:function(){
			this.fetchRange(5);
		},
		fetchRange:function(length){
		
			var picasaHelper=Environment.binPath+'\\MyHelper\\PicasaHelper.exe';
			
			var fetchingEndIndex=this.fetchingFromIndex+length;
			for(;this.fetchingFromIndex<fetchingEndIndex;++this.fetchingFromIndex){
				var accountInfo=$('#repositorySelectBox option:selected').get(0).info.getPicasaAccountPassword(this.fetchingFromIndex);
				var cmd=picasaHelper+' getFreeSpace /a '+accountInfo.userName+' /p '+accountInfo.password;

				var newRow=$('<tr><td class="tdAccount"></td><td class="tdQuotaLeft">checking</td></tr>');
				newRow.children('.tdAccount').text(accountInfo.userName);
				newRow.appendTo($('#tblPicasaWebAccount tbody'));

				AsyncProcessHelper.exec(
					[
						'script/jQuery/jquery.js',
						'script/jQuery/jquery-include.js',
						'script/tools.js'
					],
					function(p){
						var ret=Shell.runHiddenCommand(p.cmd);
						p.onFinish(ret);
					},
					{
						userName:accountInfo.userName,
						cmd:cmd,
						row:newRow,
						onFinish:function(ret){
							var out=ret.out;
							if(out.indexOf('quotalimit:')>0)
							{
								//successfully get quota info
								var leftSizeInMb=parseInt((parseInt(/quotalimit:(\d+)/g.exec(out)[1])-parseInt(/quotacurrent:(\d+)/g.exec(out)[1]))/1048576);
								this.row.children('.tdQuotaLeft').text(leftSizeInMb);
								
								//check whether this is the latest row
								{
									var tbody=this.row.parent().get(0);
									
									if(tbody.rows[tbody.rows.length-1]==this.row.get(0)){
										uploadToPicasaWebStep.accountInfoFetcher.continueFetch();
									}
								}
							}else{
								// no this account,kill the row
								this.row.remove();
							}
						}
					},
					false
				);
				continue;				
		
			}
		},
		fetchingFromIndex:0
	}
};
	
stepManager.add(uploadToPicasaWebStep);
